//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.preparation;

import java.awt.*;
import javax.swing.*;
import org.jpgolay.gui.fixed.LabelAndDateButton;
import org.jpgolay.gui.fixed.TableViewer;

public class PreparationBatchGUI extends JPanel {

    public PreparationBatchGUI() {
        selectedRowPanel = null;
        tablePanel = null;
        tableViewer = null;
        infoPanel = null;
        itemIdLabel = null;
        itemDesignationLabel = null;
        pieceLabel = null;
        itemQuantityLabel = null;
        infoTitleLabel = null;
        infoTotalLabel = null;
        labelAndDateButton = null;
        initialize();
    }

    private void initialize() {
        setLayout(new BorderLayout());
        setSize(316, 200);
        add(getLabelAndDateButton(), "North");
        add(getTablePanel(), "Center");
        add(getSelectedRowPanel(), "South");
    }

    private JPanel getSelectedRowPanel() {
        if (selectedRowPanel == null) {
            FlowLayout flowLayout1 = new FlowLayout();
            selectedRowPanel = new JPanel();
            selectedRowPanel.setLayout(flowLayout1);
            flowLayout1.setHgap(2);
            flowLayout1.setVgap(0);
            selectedRowPanel.setBackground(Color.white);
            selectedRowPanel.setBorder(BorderFactory.createLineBorder(Color.orange, 3));
            selectedRowPanel.add(getItemIdLabel(), null);
            selectedRowPanel.add(getItemDesignationLabel(), null);
            selectedRowPanel.add(getPieceLabel(), null);
            selectedRowPanel.add(getItemQuantityLabel(), null);
        }
        return selectedRowPanel;
    }

    private JPanel getInfoPanel() {
        if (infoPanel == null) {
            FlowLayout flowLayout2 = new FlowLayout();
            infoPanel = new JPanel();
            infoPanel.setLayout(flowLayout2);
            flowLayout2.setHgap(2);
            flowLayout2.setVgap(2);
            flowLayout2.setAlignment(1);
            infoPanel.add(getInfoTitleLabel(), null);
            infoPanel.add(getInfoTotalLabel(), null);
        }
        return infoPanel;
    }

    private JLabel getItemIdLabel() {
        if (itemIdLabel == null) {
            itemIdLabel = new JLabel();
            itemIdLabel.setText("itemId");
            itemIdLabel.setPreferredSize(new Dimension(70, 22));
            itemIdLabel.setHorizontalAlignment(2);
            itemIdLabel.setToolTipText("R\351f\351rence");
        }
        return itemIdLabel;
    }

    private JLabel getItemDesignationLabel() {
        if (itemDesignationLabel == null) {
            itemDesignationLabel = new JLabel();
            itemDesignationLabel.setText("itemDesignation");
            itemDesignationLabel.setPreferredSize(new Dimension(120, 22));
            itemDesignationLabel.setToolTipText("D\351signation");
        }
        return itemDesignationLabel;
    }

    private JLabel getPieceLabel() {
        if (pieceLabel == null) {
            pieceLabel = new JLabel();
            pieceLabel.setText("piece");
            pieceLabel.setPreferredSize(new Dimension(60, 22));
            pieceLabel.setHorizontalAlignment(0);
            pieceLabel.setToolTipText("Pi\350ce");
        }
        return pieceLabel;
    }

    private JLabel getItemQuantityLabel() {
        if (itemQuantityLabel == null) {
            itemQuantityLabel = new JLabel();
            itemQuantityLabel.setText("itemQuantity");
            itemQuantityLabel.setPreferredSize(new Dimension(50, 22));
            itemQuantityLabel.setHorizontalAlignment(4);
            itemQuantityLabel.setToolTipText("Quantit\351");
        }
        return itemQuantityLabel;
    }

    private JLabel getInfoTitleLabel() {
        if (infoTitleLabel == null) {
            infoTitleLabel = new JLabel();
            infoTitleLabel.setText("Quantit\351 s\351l\351ctionn\351e");
            infoTitleLabel.setPreferredSize(new Dimension(207, 22));
            infoTitleLabel.setFont(new Font("Arial", 1, 12));
            infoTitleLabel.setHorizontalAlignment(2);
            infoTitleLabel.setForeground(new Color(0, 0, 204));
        }
        return infoTitleLabel;
    }

    private JLabel getInfoTotalLabel() {
        if (infoTotalLabel == null) {
            infoTotalLabel = new JLabel();
            infoTotalLabel.setText("0.00");
            infoTotalLabel.setPreferredSize(new Dimension(100, 22));
            infoTotalLabel.setHorizontalAlignment(4);
            infoTotalLabel.setToolTipText("Le total doit correspondre \340 la quantit\351 de la ligne s\351l\351ctionn\351e");
            infoTotalLabel.setFont(new Font("Arial", 1, 12));
            infoTotalLabel.setForeground(new Color(0, 0, 204));
        }
        return infoTotalLabel;
    }

    private JPanel getTablePanel() {
        if (tablePanel == null) {
            tablePanel = new JPanel();
            tablePanel.setLayout(new BorderLayout());
            tablePanel.add(getTableViewer(), "Center");
        }
        return tablePanel;
    }

    private TableViewer getTableViewer() {
        if (tableViewer == null) {
            tableViewer = new TableViewer();
            tableViewer.setToolTipText("");
            tableViewer.add(getInfoPanel(), "South");
        }
        return tableViewer;
    }

    private LabelAndDateButton getLabelAndDateButton() {
        if (labelAndDateButton == null) {
            labelAndDateButton = new LabelAndDateButton();
            labelAndDateButton.setIconButton(new ImageIcon(getClass().getResource("/org/jpgolay/gui/iconBar/images/save.png")));
            labelAndDateButton.setLabelLeft("Lots disponible");
            labelAndDateButton.setToolTipTextButton("Enregistrer la ligne");
        }
        return labelAndDateButton;
    }

    public TableViewer getTableViewerPanel() {
        return getTableViewer();
    }

    public JButton getSaveButton() {
        return getLabelAndDateButton().getButtonRight();
    }

    public void cleanLabel() {
        getInfoTotalLabel().setText("0.00");
        getItemQuantityLabel().setText("");
        getPieceLabel().setText("");
        getItemDesignationLabel().setText("");
        getItemIdLabel().setText("");
    }

    public void setSelectedRow(String itemId, String designation, String pieceId, String quantity) {
        getItemQuantityLabel().setText(quantity);
        getPieceLabel().setText(pieceId);
        getItemDesignationLabel().setText(designation);
        getItemIdLabel().setText(itemId);
    }

    public String getTotalVal() {
        return getInfoTotalLabel().getText();
    }

    public void setTotalVal(String totalVal) {
        getInfoTotalLabel().setText(totalVal);
    }
    private JPanel selectedRowPanel;
    private JPanel tablePanel;
    private TableViewer tableViewer;
    private JPanel infoPanel;
    private JLabel itemIdLabel;
    private JLabel itemDesignationLabel;
    private JLabel pieceLabel;
    private JLabel itemQuantityLabel;
    private JLabel infoTitleLabel;
    private JLabel infoTotalLabel;
    private LabelAndDateButton labelAndDateButton;
}
